<?php

class Automatmodel extends CI_Model{
	
	var $conn;
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$user = "automatsystem";
		$pass = "freemachineskillhumans";
		error_reporting(0);
		$this->conn = mysql_connect("localhost:3306",$user,$pass);
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3307",$user,$pass);	
		}
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3308",$user,$pass);	
		}
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3309",$user,$pass);
		}
		error_reporting(E_ALL);
		if(!$this->conn){
			echo '*ERROR* Usermodel: Can\'t connect to MySQL server.';
		}
		else{
			mysql_select_db("automatsystem");
		}
    }

	
	function testConnection(){
		if($this->conn){return true;}
		return false;
	}
	
	/*
		Returns next fields : AutomatId,ConnectTime,Latitude,Longitude,IP,UserId,Balance,Serviceability,Info
		Example:
			$query = $this->Automatmodel->getAllAutomats();
			echo $query[0]['AutomatId'];
	*/
	function getAllAutomats($userId){
		if($userId){
			$queryRes = mysql_query("SELECT * FROM Automat WHERE UserId=".$userId.";",$this->conn);
		}
		else{
			$queryRes = mysql_query("SELECT * FROM Automat;",$this->conn);
		}
		$retArr = array();
		while($row = mysql_fetch_assoc($queryRes)){
			$retArr[] = $row;
		}
		return $retArr;
	}
	
	//datetime ("yyyy-MM-dd hh:mm:ss"), numeric(4,2), numeric(4,2), string, int, double(10,2), bool
	function addAutomat($connectTime,$latitude,$longitude,$IP,$userId,$balance,$serviceability,$info){
		
        $columnsString = '';
        $valuesString = '';
		$valuePresent = false;
		if($connectTime){
			$columnsString .= "ConnectTime";
			$valuesString .= "'".$connectTime."'";
			$valuePresent = true;
		}
		if($latitude){
			if($valuePresent){ 
				$columnsString .= ',';
				$valuesString.= ',';
			}
			$columnsString .= "Latitude";
            $valuesString .= "'".$latitude."'";
			$valuePresent = true;
		}
		if($longitude){
			if($valuePresent){ 
                            $columnsString .= ',';
                            $valuesString .= ',';
                        }
			$columnsString .= "Longitude";
			$valuesString .= "'".$longitude."'";
			$valuePresent = true;
		}
                if($IP){
			if($valuePresent){ 
                            $columnsString .= ',';
                            $valuesString .= ',';
                        }
			$columnsString .= "IP";
                        $valuesString .= "'".$IP."'";
			$valuePresent = true;
		}
                if($userId){
			if($valuePresent){ 
                            $columnsString .= ',';
                            $valuesString .= ',';
                        }
			$columnsString .= "UserId";
                        $valuesString .= $userId;
			$valuePresent = true;
		}
                if($balance){
			if($valuePresent){ 
                            $columnsString .= ',';
                            $valuesString .= ',';
                        }
			$columnsString .= "Balance";
                        $valuesString .=$balance;
			$valuePresent = true;
		}
                if($serviceability){
			if($valuePresent){ 
                            $columnsString .= ',';
                            $valuesString .= ',';
                        }
			$columnsString .= "Serviceability";
                        $valuesString .= ($serviceability ? '(1)' : '(0)');
			$valuePresent = true;
		}
                if($info){
			if($valuePresent){ 
                            $columnsString .= ',';
                            $valuesString .= ',';
                        }
			$columnsString .= "Info";
                        $valuesString .= "'".$info."'";
			$valuePresent = true;
		}
		$res = mysql_query("INSERT INTO Automat(".$columnsString.")
		VALUES(".$valuesString.");",$this->conn);
		if(!$res){echo '<p>Error adding automat. </p>'; echo(mysql_error());}
		return $res;
	}

	//int
	function deleteAutomat($automatId){
		$res = mysql_query("DELETE FROM Automat WHERE AutomatId=".$automatId.";");
		if(!$res){echo 'Error deleting automat. '.mysql_error();}
		return $res;
	}
	
	function lastAutomatId() {
		$query = mysql_query("SELECT MAX(AutomatId) FROM Automat GROUP BY UserId;");
		$lastAutomatId;
		while ($row = mysql_fetch_array($query)) {
			$lastAutomatId = $row['MAX(AutomatId)'];
		}
		return $lastAutomatId;
	}
	
	//int, datetime ("yyyy-MM-dd hh:mm:ss")
	function setConnectTime($automatId, $connectTime){
		$res = mysql_query("UPDATE Automat SET ConnectTime='".$connectTime."' WHERE AutomatId=".$automatId.";");
		if(!$res){echo '<p>Error updating connect time. AutomatId='.$automatId.'. </p>';}
		return $res;
	}
	
	function setPosition($automatId, $latitude, $longitude){
		$res = mysql_query("UPDATE Automat SET Latitude='".$latitude."', Longitude='".$longitude."' WHERE AutomatId=".$automatId.";");
		if(!$res){echo '<p>Error updating position. AutomatId='.$automatId.'. </p>';}
		return $res;
	}
	
	function setIP($automatId, $IP){
		$res = mysql_query("UPDATE Automat SET IP='".$IP."' WHERE AutomatId=".$automatId.";");
		if(!$res){echo '<p>Error updating IP. AutomatId='.$automatId.'. </p>';}
		return $res;
	}
	
	function setBalance($automatId, $balance){
		$res = mysql_query("UPDATE Automat SET Balance='".$balance."' WHERE AutomatId=".$automatId.";");
		if(!$res){echo '<p>Error updating balance. AutomatId='.$automatId.'. </p>';}
		return $res;
	}
	
	function setServiceability($automatId, $serviceability){
		$res = mysql_query("UPDATE Automat SET Serviceability='".$serviceability."' WHERE AutomatId=".$automatId.";");
		if(!$res){echo '<p>Error updating serviceability. AutomatId='.$automatId.'. </p>';}
		return $res;
	}
        
        function setInfo($automatId, $info){
            if (strlen($info)>100){
                echo '<p>Error : info to long.</p>';
                return;
            }
            $res = mysql_query("UPDATE Automat SET Info='".$info."' WHERE AutomatId=".$automatId.";");
            if(!$res){echo '<p>Error updating info. AutomatId='.$automatId.'. </p>';}
            return $res;
	}
	
	function getAutomat($automateId){
            $res = mysql_query("SELECT * FROM Automat WHERE AutomatId=".$automateId.";");
            return mysql_fetch_array($res);
        }
	
	
	
	
	
}